package com.pxxy.yuyi.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pxxy.yuyi.entity.Admin;
import com.pxxy.yuyi.entity.Leave;
import com.pxxy.yuyi.service.LeaveService;
import com.pxxy.yuyi.utils.ConstUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by IntelliJ IDEA
 *
 * @Author : XuWen
 * @create 2021/6/21 0:44
 */
@Controller
@RequestMapping("/leave")
public class LeaveController {
    @Autowired
    private LeaveService leaveService;
    //前台提交留言信息
    @RequestMapping("/setup")
    public String setup(Leave leave, HttpServletRequest request){
        Integer res = leaveService.insertLeave(leave);
        if(res==null){
            request.setAttribute("mes","提交失败！");
            return "faqMessage";
        }
        request.setAttribute("mes","提交成功");
        return "faqMessage";
    }
    //前端展示留言信息
    @RequestMapping("/show")
    public String show(HttpServletRequest request,Integer pageNum){
        if(pageNum==null){
            pageNum=1;
        }
        PageInfo<Leave> list =leaveService.selectLeave(pageNum);
        request.setAttribute("pi",list);
        return "faqMessage";
    }
    //后台显示留言信息
    @RequestMapping("/showlist")
    public String show(Integer pageNum,HttpServletRequest request ){
        if (pageNum==null){
            pageNum=1;
        }
        Admin admin = (Admin) request.getSession().getAttribute(ConstUtils.LOGIN_USER);
        if (admin==null) {//为空则先进行登录
            request.setAttribute("msg", "请先登录");
            return "admin/adminLogin";
        }
        PageInfo<Leave> info = leaveService.selectLeaves(pageNum);
        request.setAttribute("pi",info);
        return "admin/leave";
    }
    //后台删除留言
    @RequestMapping("/deleteLeave")
    public String deleteLeave(Integer lid){
        Integer result=  leaveService.deleteLeave(lid);
        return "forward:/leave/showlist";
    }
    //后台回复留言
    @RequestMapping("/update")
    public String update(Leave leave){
        Integer result= leaveService.update(leave);
        return "forward:/leave/showlist";
    }
}
